﻿@*Displays the source file on the far right panel of the app*@

@page "/SourceFile"
@using Microsoft.Build.Logging.StructuredLogger
@using StructuredLogViewer
@using System.IO
@using BlazorMonaco
@using BlazorMonaco.Bridge

    <div class="flexSource">
        <div class="flex-container">
            <h2 style="margin:auto">@ContainerSplit.sourceFileName</h2>
            <button @onclick=Exit class="btn btn-secondary" id="closeButton">Close</button>
        </div>

        <MonacoEditor @ref="_editor"
                      CssClass="code"
                      Id="monaco-editor"
                      ConstructionOptions="EditorConstructionOptions"
                      OnDidInit="@((editor) => EditorOnDidInit((MonacoEditor)editor))" />
    </div>
@code {
    [CascadingParameter]
    public SplitPane ContainerSplit { get; set; }
    public MonacoEditor _editor { get; set; }

    /// <summary>
    /// Defines how the monaco editor should look including what text to display
    /// </summary>
    /// <param name="editor"> The editor being created </param>
    /// <returns> The construction options for the editor </returns>
    private StandaloneEditorConstructionOptions EditorConstructionOptions(MonacoEditor editor)
    {
        string theme;
        if (ContainerSplit.darkMode)
        {
            theme = "vs-dark";
        } else
        {
            theme = "vs";
        }
        return new StandaloneEditorConstructionOptions()
        {
            ReadOnly = true,
            Value = ContainerSplit.sourceFileText,
            AutomaticLayout = true,
            ScrollBeyondLastLine = false,
            Language = ContainerSplit.sourceFileExtension,
            Theme = theme,
            Scrollbar = new ScrollbarOptions
            {
                Vertical = "visible",
                Horizontal = "visible"
            }
        };

    }

    /// <summary>
    /// Changes theme to dark or light mode
    /// </summary>
    /// <param name="editor"></param>
    /// <returns></returns>
    public async System.Threading.Tasks.Task changeTheme()
    {
        string theme;
        if (ContainerSplit.darkMode)
        {
            theme = "vs-dark";
        }
        else
        {
            theme = "vs";
        }
        MonacoEditor.SetTheme(theme);
    }

    /// <summary>
    /// Updates which line to be highlighted if any
    /// </summary>
    /// <param name="editor"> editor the highlighting should be changed for </param>
    /// <returns></returns>
    public async System.Threading.Tasks.Task changeHighlight(MonacoEditor editor)
    {
        await EditorOnDidInit(editor);
    }

    /// <summary>
    /// Determines how the file should be highlighted if any
    /// </summary>
    /// <param name="editor"> editor to add highlighting to</param>
    /// <returns> task to update the app </returns>
    private async System.Threading.Tasks.Task EditorOnDidInit(MonacoEditor editor)
    {
        if (ContainerSplit.sourceFileLineNumber != -1)
        {
            var newDecorations = new ModelDeltaDecoration[]
            {
            new ModelDeltaDecoration
            {
                Range = new BlazorMonaco.Bridge.Range(ContainerSplit.sourceFileLineNumber,1,ContainerSplit.sourceFileLineNumber,1),
                Options = new ModelDecorationOptions
                {
                    IsWholeLine = true,
                    ClassName = "decorationContentClass",
                }
            }
                };
            await _editor.RevealLineInCenter(ContainerSplit.sourceFileLineNumber, ScrollType.Smooth);
            await _editor.DeltaDecorations(null, newDecorations);
            await _editor.RevealLineInCenter(ContainerSplit.sourceFileLineNumber, ScrollType.Smooth);

            ContainerSplit.sourceFileLineNumber = -1;
        }


    }

    /// <summary>
    /// Allows other tabs to update the page
    /// </summary>
    public void Render()
    {
        this.StateHasChanged();
        _editor.SetValue(ContainerSplit.sourceFileText);
    }

    /// <summary>
    /// Fired when a user clicks the Close button
    /// closes the third panel and sets the app back to 2 panels
    /// </summary>
    /// <returns></returns>
    public async System.Threading.Tasks.Task Exit()
    {
        ContainerSplit.showSourceFilePanel = false;
        ContainerSplit.Render();
    }
}
